package view.components
{
	import assets.view.components.GeneralButtonViewSymbol;
	
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFieldAutoSize;
	
	import model.vo.ViewVO;

	public class GeneralButtonView extends GeneralButtonViewSymbol
	{
		public static const NAME:String 					= "GeneralButtonView";
		
		public static const CLICKED:String					= NAME + "Clicked";
		
		private var viewData:ViewVO;
		private var labelTxt:String;
		
		public function GeneralButtonView()
		{
		}
		
		public function init( viewVO:ViewVO, txt:String ):void
		{
			viewData = viewVO;
			labelTxt = txt;
			
			setGeneralButtonViewProperties();
			buildGeneralButtonView();
		}
		
		public function reset():void
		{
			FX.tint( bckgrdContainer, viewData.generalBtnColor );
			FX.tint( label, viewData.labelColor );
			addEvents();
		}
		
		public function die():void
		{
			removeEvents();
		}
		
		private function setGeneralButtonViewProperties():void
		{
		}
		
		private function buildGeneralButtonView():void
		{
			label.autoSize = TextFieldAutoSize.LEFT;
			label.text = labelTxt;
			
			paintBckgrdContainer();
			addEvents();
			paintCover();
			
			label.x = Math.round( ( bckgrdContainer.width / 2 ) - ( label.width / 2 ) );
			label.y = Math.round( ( bckgrdContainer.height / 2 ) - ( label.height / 2 ) );
		}
		
		private function addEvents():void
		{
			this.buttonMode = true;
			this.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			this.addEventListener( MouseEvent.MOUSE_OVER, mouseOverHandler );
			this.addEventListener( MouseEvent.MOUSE_OUT, mouseOutHandler );
		}
		
		private function removeEvents():void
		{
			this.buttonMode = false;
			this.removeEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			this.removeEventListener( MouseEvent.MOUSE_OVER, mouseOverHandler );
			this.removeEventListener( MouseEvent.MOUSE_OUT, mouseOutHandler );
		}
		
		private function paintBckgrdContainer():void
		{
			var g:Graphics = bckgrdContainer.graphics;
			g.clear();
			g.beginFill( viewData.generalBtnColor, 1 );
			g.drawRect( 0, 0, label.width + 10, label.height + 10 );
			g.endFill();
			
			if( bckgrdContainer.width < 54 ) bckgrdContainer.width = 54;
		}
		
		private function paintCover():void
		{
			var g:Graphics = cover.graphics;
			g.clear();
			g.beginFill( 0xff0000, 0 );
			g.drawRect( 0, 0, bckgrdContainer.width, bckgrdContainer.height );
			g.endFill();
		}
		
		private function mouseDownHandler( e:MouseEvent ):void
		{
			FX.tint( bckgrdContainer, viewData.generalBtnColor );
			FX.tint( label, viewData.labelColor );
			removeEvents();
			
			dispatchEvent( new Event( CLICKED ) );
		}
		
		private function mouseOverHandler( e:MouseEvent ):void
		{
			FX.tint( bckgrdContainer, viewData.generalBtnOverColor );
			FX.tint( label, viewData.labelSelectedColor );
		}
		
		private function mouseOutHandler( e:MouseEvent ):void
		{
			FX.tint( bckgrdContainer, viewData.generalBtnColor );
			FX.tint( label, viewData.labelColor );
		}
	}
}